package com.example.demo6.ui.pickup;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;

import com.example.demo6.SQLite.LostGoods;
import com.example.demo6.SQLite.PickupGoods;
import com.example.demo6.R;
import com.example.demo6.ui.lose.LostAdapter;


import java.util.List;

public class PickupAdapter extends RecyclerView.Adapter<PickupAdapter.ViewHolder>{
    private List<PickupGoods> mData;
    private LayoutInflater layoutInflater;
    Context context;

    public PickupAdapter(Context context, List<PickupGoods> mData){
        this.context=context;
        layoutInflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.mData = mData;
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        public TextView ShowPickupID;
        public TextView ShowPickupTime;
        public TextView ShowPickupItem;
        public TextView ShowPickupRegion;
        public ViewHolder(View convertView) {
            super(convertView);
            ShowPickupID=convertView.findViewById(R.id.ShowPickupID);
            ShowPickupTime=convertView.findViewById(R.id.ShowPickupTime);
            ShowPickupItem = convertView.findViewById(R.id.ShowPickupItem);
            ShowPickupRegion =convertView.findViewById(R.id.ShowPickupRegion);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v= layoutInflater.inflate(R.layout.pickup_list_item,parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull PickupAdapter.ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        holder.ShowPickupID.setText(Integer.toString(mData.get(position).PickupId));
        holder.ShowPickupTime.setText(mData.get(position).pickupTime);
        holder.ShowPickupItem.setText(mData.get(position).pickupGoods);
        holder.ShowPickupRegion.setText(mData.get(position).pickupPlace);

        //导航到detailfragment
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavController navController= Navigation.findNavController((Activity) context,R.id.nav_host_fragment_activity_main);
                Bundle bundle=new Bundle();
                    bundle.putInt("PickupId",mData.get(position).PickupId);
                navController.navigate(R.id.pickupdetailFragment,bundle);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }
}